package com.cpi.cornpp.product.services;

import java.util.ArrayList;
import java.util.List;

import com.cpi.cornpp.common.exception.CornPPException;
import com.cpi.cornpp.product.converter.ProductConverter;
import com.cpi.cornpp.product.dao.ProductDAO;
import com.cpi.cornpp.product.dto.ProductDTO;
import com.cpi.cornpp.product.exception.ProductException;
import com.cpi.cornpp.product.view.ProductView;

public class ProductService {

	public ProductView getProductById(String productId)throws ProductException {
		ProductDAO productDAO = null;
		ProductConverter productConverter = null;
		ProductDTO productDTO = null;
		ProductView productView = null;
		try{
			productDAO = new ProductDAO();
			productConverter = new ProductConverter();
			productDTO = productDAO.getProductById(new ProductDTO(productId));
			productView = productConverter.convert(productDTO);
		}catch (ProductException productException) {			
			productException.printStackTrace(System.out);
			productException= new ProductException(productException , CornPPException.LAYER_SERVICE,CornPPException.ACTION_LISTS);
			throw productException;

		}
		catch (Exception e){
			ProductException productException=null;
			e.printStackTrace(System.out);
			productException= new ProductException(e , CornPPException.LAYER_SERVICE,CornPPException.ACTION_LISTS);
			throw productException;
		}
		return productView;
	}
	
	
	public List getProductList(ProductView productView) throws ProductException {
		ProductDAO productDAO = null;
		ProductConverter productConverter = null;
		ProductDTO productDTO = null;
		List collectionProductDTO = null;
		List collectionProductView = null;
		try {
			productDAO = new ProductDAO();
			productDTO = new ProductDTO();
			productConverter = new ProductConverter();
			collectionProductDTO = new ArrayList(0);
			collectionProductView = new ArrayList(0);
			
			// La vista la convierto en un DTO
			productDTO = productConverter.convert(productView);
			// Obtengo el listado de los clientes
			collectionProductDTO = productDAO.list(productDTO);
			// Mando convertir el listado en la vista de retorno
			collectionProductView = productConverter.convertDTOs(collectionProductDTO);
		} catch (ProductException productException) {			
			productException.printStackTrace(System.out);
			productException= new ProductException(productException , CornPPException.LAYER_SERVICE,CornPPException.ACTION_LISTS);
			throw productException;

		}
		catch (Exception e){
			ProductException productException=null;
			e.printStackTrace(System.out);
			productException= new ProductException(e , CornPPException.LAYER_SERVICE,CornPPException.ACTION_LISTS);
			throw productException;
		}
		return collectionProductView;
	}
	
	public List getCurrentForescastedProductList(String clientId) throws ProductException {
		List productList = null;
		try{
			productList = new ProductConverter().convertDTOs(new ProductDAO().getCurrentForescastedProductList(clientId));
		} catch (ProductException productException) {			
			productException.printStackTrace(System.out);
			productException= new ProductException(productException , CornPPException.LAYER_SERVICE,CornPPException.ACTION_LISTS);
			throw productException;
		}
		catch (Exception e){
			ProductException productException=null;
			e.printStackTrace(System.out);
			productException= new ProductException(e , CornPPException.LAYER_SERVICE,CornPPException.ACTION_LISTS);
			throw productException;
		}
		return productList;
	}
	
}
